import 'dart:io';

import 'package:get/get.dart';
import 'package:just_audio/just_audio.dart';
import 'package:squat_easy/se_constant.dart';
import 'package:squat_easy/se_service/se_user_service.dart';
import 'package:squat_easy/se_voice_record/se_voice_record.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

class SeMineLogic extends GetxController with CommonLife, OnceClick {
  String audioName = '';

  ///音频播放器
  final AudioPlayer player = AudioPlayer();

  @override
  void onReady() {
    super.onReady();
    SeUserService.instance.getCacheMineInfoBean();
    audioName = CommonStorage().getString(SeStorageKey.spKeyAudio);
    update();
  }

  void saveAudio(String fileName) async {
    await CommonStorage().setString(SeStorageKey.spKeyAudio, fileName);
    audioName = fileName;
    update();
  }

  void playAudio() async {
    try {
      String directory = await SeFileUtils.directory;
      String path = '$directory/$audioName';
      bool exist = await File(path).exists();
      if (Loading.predicate(() => !exist, 'Audio not exist')) return;
      if (player.playing) await player.stop();
      await player.setFilePath(path);
      player.play();
    } catch (e) {}
  }

  @override
  void pageBack() {
    super.pageBack();
    SeUserService.instance.getCacheMineInfoBean(true);
  }
}
